<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    append-to-body
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible="visible"
    width="600px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="分享标题" prop="shareTitle">
        <el-input
          v-model="dataForm.shareTitle"
          placeholder="请输入分享标题"
        ></el-input>
      </el-form-item>
      <el-form-item label="分享主图" prop="shareImage">
         <ImageUploadSingle v-model="dataForm.shareImage"></ImageUploadSingle>
      </el-form-item>
      <el-form-item label="分享种类:" prop="shareCate">
        <el-radio-group v-model="dataForm.shareCate">
          <el-radio label="1" border>分享好友</el-radio>
          <el-radio label="2" border>分享朋友圈</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="上架状态:" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio label="1" border>上架</el-radio>
          <el-radio label="0" border>下架</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" :rows="4" v-model="dataForm.remark" placeholder="请输入备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit"
        >确定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from "@/api/shop_config/share";
import ImageUploadSingle from '@/components/imageUploadSingle';
export default {
  props:{
    visible: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      canSubmit: false,
      dataForm: {
        shareImage:null,
        status:'1'
      },
      dataRule: {
        shareTitle: [
          { required: true, message: "分享标题不能为空", trigger: "blur" },
        ],
        shareImage: [
          { required: true, message: "分享主图不能为空", trigger: "blur" },
        ],
        shareCate: [
          { required: true, message: "分享种类不能为空", trigger: "blur" },
        ],
        status: [
          { required: true, message: "上架状态不能为空", trigger: "blur" },
        ]

      },
    };
  },
  components: {
    ImageUploadSingle
  },
  methods: {
    init(id) {
      this.canSubmit = true;
      if(id){
        getObj(id).then((response) => {
          let data = response.data.data;
          if(data.shareImage){
            let arr = JSON.parse(data.shareImage)
            if(arr.length > 0){
              data.shareImage = arr[0]
            }
          }
          this.dataForm = data

        });
      }
      
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          let requestData = JSON.parse(JSON.stringify(this.dataForm))
          requestData.shareImage = requestData.shareImage.materialId

          if (requestData.id) {
            putObj(requestData)
              .then((data) => {
                this.$notify.success("修改成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          } else {
            addObj(requestData)
              .then((data) => {
                this.$notify.success("添加成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          }
        }
      });
    },
    //重置表单
    closeDialog() {
      this.$emit('update:visible', false)
    },
  },
};
</script>
